﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Text;
using Portal.Environment;
using Portal.Language;
using Portal.Interface;

namespace Portal.CMS
{
    public class UpdateGroupNews : WebPart
    {
        public override string WebPartId
        {
            get
            {
                return "UpdateGroupNews";
            }
        }

        public override string Title
        {
            get
            {
                return ResourceBLL.GetText(WebPartId,"Title","Cập nhật nhóm tin tức!");
            }
        }

        public override string Description
        {
            get
            {
                return ResourceBLL.GetText(WebPartId,"Description","");
            }
        }

        public override string RenderWebPart(LoadParamCls loadParamCls)
        {
            AjaxPro.Utility.RegisterTypeForAjax(typeof(UpdateGroupNews));
            string GroupNewsId = System.Web.HttpContext.Current.Request["GroupNewsId"];
            GroupNewsCls GroupNewsCls = new GroupNewsBLL().CreateModel(GroupNewsId);
            if (GroupNewsCls == null)
            {
                return Environment.Environments.GetRecordNotFound();
            }


            string RetHtml =
                   WebPartUtilities.AttachJavascript("Portal.CMS", "GroupNews.js") +
                       Environments.GetFormDialog(
                             "      <table>" +
                             "          <tr>" +
                             "              <td>" + ResourceBLL.GetText(WebPartId, "UpdateGroupNewsCaption", "Nhóm tin") + "</td>" +
                             "              <td><input class=cssTextBoxDialog id=txtGroupNews value=\"" + GroupNewsCls.Subject + "\"></td>" +
                             "          </tr>" +
                             "          <tr>" +
                             "              <td>" + ResourceBLL.GetText(WebPartId, "IsNews", "Nhóm tin") + "</td>" +
                             "              <td><input type=checkbox id=chkIsNews " + (GroupNewsCls.IsNews==1?"CHECKED":"") + "></td>" +
                             "          </tr>" +
                             "          <tr>" +
                             "              <td></td>" +
                             "              <td>(*)" + ResourceBLL.GetText(WebPartId, "Help", "Tích vào nhóm tin nếu đây là folder chứa tin tức") + "</td>" +
                             "          </tr>" +
                             "          <tr>" +
                             "              <td></td>" +
                             "              <td><input type=button class=cssSaveButton onclick=javascript:ClientSaveUpdateGroupNews('" + GroupNewsId + "'); value=\"" + ResourceBLL.GetText(WebPartId, "UpdateGroupNewsSaveButton", "Ghi lại") + "\"><input type=button class=cssCancelButton onclick=javascript:window.close(); value=\"" + ResourceBLL.GetText(WebPartId, "UpdateGroupNewsCancelButton", "Thoát") + "\"></td>" +
                             "          </tr>" +
                             "      </table>", ResourceBLL.GetText(WebPartId, "UpdateGroupNewsFormTitle", "Cập nhật nhóm tin tức"));
            return RetHtml;
        }

        [AjaxPro.AjaxMethod]
        public string ServerSideUpdateGroupNews(
            string GroupNewsId, 
            string GroupNewsName,
            bool   IsNews)
        {
            try
            {
                Environment.Environments.CheckSessionTimeOut(true);
                if (GroupNewsName.Trim().Length == 0) throw new Exception(ResourceBLL.GetText(WebPartId,"EnterName","Nhập tên nhóm tin"));
                new GroupNewsBLL().UpdateGroupNews(null, GroupNewsId, GroupNewsName, IsNews?1:0);
                return ResourceBLL.GetText(WebPartId,"MessageUpdateOk","Cập nhật thành công!");
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }
    }
}